package com.andy.ui.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.andy.ui.R;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

/**
 * @Author: luofan
 * @CreateDate: 2020/3/30 11:23
 * @Description:
 */
public class PageStateView extends FrameLayout {

    /**
     * 只显示内容
     */
    public static final int STATE_CONTENT = 0;
    /**
     * 只显示加载中
     */
    public static final int STATE_LOADING = 1;
    /**
     * 只显示空页面
     */
    public static final int STATE_EMPTY = 2;
    /**
     * 只显示错误
     */
    public static final int STATE_ERROR = 3;
    /**
     * 显示内容并显示加载中
     */
    public static final int STATE_CONTENT_LOADING = 4;

    private static int sDefaultLoadingLayoutId = R.layout.default_loading_view;
    private static int sDefaultEmptyLayoutId = R.layout.default_empty_layout;
    private static int sDefaultErrorLayoutId = R.layout.default_error_view;

    private View mLoadingView;
    private View mEmptyView;
    private View mErrorView;
    private ViewGroup mStateLayout;
    private int mState;


    public PageStateView(@NonNull Context context) {
        this(context, null);
    }

    public PageStateView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public PageStateView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initTypeArray(context, attrs);
    }

    private void initTypeArray(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.PageStateView);
        int loadingLayoutId = typedArray.getResourceId(R.styleable.PageStateView_loadingLayoutId, sDefaultLoadingLayoutId);
        int emptyLayoutId = typedArray.getResourceId(R.styleable.PageStateView_emptyLayoutId, sDefaultEmptyLayoutId);
        int errorLayoutId = typedArray.getResourceId(R.styleable.PageStateView_errorLayoutId, sDefaultErrorLayoutId);
        mState = typedArray.getInt(R.styleable.PageStateView_state, STATE_LOADING);
        typedArray.recycle();
        mLoadingView = LayoutInflater.from(context).inflate(loadingLayoutId, null);
        mEmptyView = LayoutInflater.from(context).inflate(emptyLayoutId, null);
        mErrorView = LayoutInflater.from(context).inflate(errorLayoutId, null);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        if (mStateLayout == null) {
            mStateLayout = (ViewGroup) LayoutInflater.from(getContext()).inflate(R.layout.layout_page_state, null);
            mStateLayout.addView(mLoadingView);
            mStateLayout.addView(mEmptyView);
            mStateLayout.addView(mErrorView);
            addView(mStateLayout);
            showViewByState(mState);
        }
    }

    public void showViewByState(int state) {
        mEmptyView.setVisibility(GONE);
        mErrorView.setVisibility(GONE);
        mLoadingView.setVisibility(GONE);
        mStateLayout.setVisibility(VISIBLE);
        switch (state) {
            case STATE_LOADING:
                mLoadingView.setVisibility(VISIBLE);
                break;
            case STATE_EMPTY:
                mEmptyView.setVisibility(VISIBLE);
                break;
            case STATE_ERROR:
                mErrorView.setVisibility(VISIBLE);
                break;
            case STATE_CONTENT_LOADING:
                //TODO 后续实现
                break;
            case STATE_CONTENT:
                mStateLayout.setVisibility(GONE);
            default:

        }
    }

    public View getEmptyView() {
        return mEmptyView;
    }

    public View getErrorView() {
        return mErrorView;
    }

    public static void initLayoutConfig(int loadingLayoutId, int emptyLayoutId, int errorLayoutId) {
        sDefaultLoadingLayoutId = loadingLayoutId;
        sDefaultEmptyLayoutId = emptyLayoutId;
        sDefaultErrorLayoutId = errorLayoutId;
    }

}
